package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TMember;
import hl.model.TPlace;
import hl.model.TVenueInfo;
import hl.pageModel.Member;
import hl.pageModel.Place;
import hl.pageModel.Bar;
import hl.pageModel.DataGrid;
import hl.service.MemberService;

@Service("memberService")
public class MemberServiceImpl implements MemberService {

	private BaseDao<TMember> memberDao;

	private BaseDao<TVenueInfo> venueInfoDao;

	public BaseDao<TVenueInfo> getVenueInfoDao() {
		return venueInfoDao;
	}

	@Autowired
	public void setVenueInfoDao(BaseDao<TVenueInfo> venueInfoDao) {
		this.venueInfoDao = venueInfoDao;
	}

	public BaseDao<TMember> getMemberDao() {
		return memberDao;
	}

	@Autowired
	public void setMemberDao(BaseDao<TMember> memberDao) {
		this.memberDao = memberDao;
	}

	@Override
	public DataGrid getDatagrid(Member member) {
		DataGrid dg = new DataGrid();
		String hql = "from TMember t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(member, hql, params);

		if (member.getOrder() != null) {
			hql += " order by " + member.getSort() + " " + member.getOrder();
		}

		List<TMember> l = memberDao.find(hql, params, member.getPage(), member.getRows());

		int totalHql = memberDao.count(hql, params);

		List<Member> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TMember t : l) {
				Member u = new Member();
				BeanUtils.copyProperties(t, u);
				u.setVenueName(t.getTVenueInfo().getName());
				u.setPkVenueInfo(t.getTVenueInfo().getPkVenueInfo());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Member member, String hql, Map<String, Object> params) {
		if (member.getName() != null && !member.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + member.getName().trim() + "%");
		}

		if (member.getGender() != null && !member.getGender().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.gender like :value2 ";
			params.put("value2", "%" + member.getGender().trim() + "%");
		}
		if (member.getPkMember() != null && !member.getPkMember().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.pkMember like :value3 ";
			params.put("value3", "%" + member.getPkMember().toString() + "%");
		}
		return hql;
	}

	@Override
	public void save(Member member) {
		TMember t = new TMember();
		BeanUtils.copyProperties(member, t);
		t.setCdatetime(new Date());
		t.setTVenueInfo(venueInfoDao.get(TVenueInfo.class, member.getPkVenueInfo()));
		memberDao.save(t);
	}

	@Override
	public void edit(Member member) {
		TMember t = new TMember();
		BeanUtils.copyProperties(member, t);
		t.setMdatetime(new Date());
		t.setTVenueInfo(venueInfoDao.get(TVenueInfo.class, member.getPkVenueInfo()));
		memberDao.saveOrUpdate(t);
	}

	@Override
	public void remove(Member member) {
		TMember t = memberDao.get(TMember.class, member.getPkMember());
		memberDao.delete(t);
	}

	@Override
	public Bar getGenderBar() {
		Bar b = new Bar();
		int[] c = new int[3];
		c[0] = memberDao.count("from TMember where gender like ? ", new Object[] { "%男%" });
		c[1] = memberDao.count("from TMember where gender like ? ", new Object[] { "%女%" });
		c[2] = 5;
		String[] cat = { "男", "女","不是人" };
		b.setCategories(cat);
		b.setValues(c);
		return b;
	}

	@Override
	public Object getComboBox(Member member) {
		List<TMember> l = memberDao.find("from TMember");
		List<Member> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TMember t : l) {
				Member u = new Member();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		return pl;
	}
}
